package com.sikaryofficial.backend.controller.information;

import com.sikaryofficial.backend.domain.dto.resp.information.ArticleWordImportDTO;
import com.sikaryofficial.backend.manager.information.WordAnalysisManager;
import com.sikaryofficial.backend.resolver.PropertiesTools;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.core.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 资讯-word导入服务
 *
 * @author qinjinyuan
 * @date 2024-04-10
 */
@RestController
@RequestMapping("/article/word")
@Api(tags = "资讯-word导入服务")
@Slf4j
public class WordAnalysisController extends BaseController {
    @Autowired
    private WordAnalysisManager wordAnalysisManager;
    @Value("${hismk.article.import.maxFileSize:5}")
    private Integer maxFileSize;

    @Autowired
    private PropertiesTools propertiesTools;

    @GetMapping("/getTestI18n/{msgKey}")
    @ApiOperation(value = "国际化测试")
    public R<String> articleWordImport(@PathVariable(value = "msgKey") String msgKey) {
        return R.ok(propertiesTools.getProperties(msgKey));
    }

    /**
     * word导入
     */
    @PostMapping("/import")
    @ApiOperation(value = "word导入")
    public R<ArticleWordImportDTO> articleWordImport(@RequestParam("file") MultipartFile word) {
        checkParam(word);
        return R.ok(wordAnalysisManager.wordAnalysis(word));
    }

    /**
     * 参数检查
     *
     * @param word
     */
    private void checkParam(MultipartFile word) {
        long fileSizeInBytes = word.getSize();
        // 转换为KB
        double fileSizeInKB = fileSizeInBytes / 1024.0;
        // 转换为MB
        double fileSizeInMB = fileSizeInKB / 1024.0;
        if (fileSizeInMB > maxFileSize) {
            throw new ServiceException("import file is large, support less or equal to " + maxFileSize + "M only");
        }
    }

}
